Skip to content

Fix SQLiteAdapter when changing primary key#954

Merged
markstory merged 3 commits into5.xfrom
5.x-sqlite-fixes
Nov 13, 2025
Merged

Fix SQLiteAdapter when changing primary key#954
markstory merged 3 commits into5.xfrom
5.x-sqlite-fixes

Conversation

@dereuromark
Copy link
Copy Markdown
Member

Issue: cakephp/phinx#2364

SQLite adapter incorrectly adds AUTOINCREMENT keyword to integer columns when changing a table's primary key, even when not explicitly specified.

Changes Made

  1. Fixed SqliteAdapter::getAddPrimaryKeyInstructions() (src/Db/Adapter/SqliteAdapter.php:1338-1372)
  • Before: Automatically added AUTOINCREMENT to ALL INTEGER columns when making them primary keys
  • After: Only adds AUTOINCREMENT if the column already had it in the original table definition
  • Uses regex to detect the presence of AUTOINCREMENT keyword in the current CREATE TABLE SQL

The fix was verified to:

  • ✅ No longer add AUTOINCREMENT when changing PK to a column that didn't have it
  • ✅ Maintain backward compatibility with existing tests
  • ✅ Match the behavior of other adapters (MySQL, Postgres, SQL Server)

Comment on lines 1358 to 1363
} elseif ($matches[2] === 'INTEGER') {
// INTEGER column without AUTOINCREMENT should stay that way
$replace = '$1 INTEGER NOT NULL PRIMARY KEY';
} else {
$replace = '$1 $2 NOT NULL PRIMARY KEY';
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this new case redundant as the case on 1362 handles this?

dereuromark and others added 2 commits November 11, 2025 17:51
Co-authored-by: Mark Story <mark@mark-story.com>
@markstory markstory merged commit d21076b into 5.x Nov 13, 2025
13 checks passed
@markstory markstory deleted the 5.x-sqlite-fixes branch November 13, 2025 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants